package com.qf.sale.controller;

import com.qf.lw.pojo.AdminPojo;
import com.qf.lw.pojo.CustomerPojo;
import com.qf.lw.util.StringUtils;
import com.qf.sale.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller

public class CustomerController {
    @Autowired
    ICustomerService customerService;
    //登陆
    @RequestMapping("login")
    public String login(CustomerPojo customerPojo, Model model,HttpSession session) {
        if (null == customerPojo) {
            model.addAttribute("errorMsg", "系统异常");
            return "forward:login.jsp";
        }
        if (StringUtils.isEmpty(customerPojo.getAname())) {
            model.addAttribute("errorMsg", "用户名不能为空");
            return "forward:login.jsp";
        }
        if (StringUtils.isEmpty(customerPojo.getApass())) {
            model.addAttribute("errorMsg", "密码不能为空");
            return "forward:login.jsp";
        }
        CustomerPojo customerPojo1 = customerService.login(customerPojo);
        if (null == customerPojo1) {
            model.addAttribute("errorMsg", "用户名密码错误");
            return "forward:login.jsp";
        }

        session.setAttribute("customer",customerPojo1);

        return "forward:index";

    }
    //注册
    @RequestMapping("regiest")
    public String regiest(CustomerPojo customerPojo , Model model, HttpSession session) {
        // CustomerPojo customerPojo1 = customerService.login(customerPojo);

        if (null == customerPojo.getAname() || "" == customerPojo.getAname()) {
            model.addAttribute("errorMsg", "用户名不能为空");
            return "forward:regiest.jsp";
        }
        CustomerPojo customerPojo1 = customerService.loregiest(customerPojo);
        if (null != customerPojo1) {
            model.addAttribute("errorMsg", "用户名已存在");
            return "forward:regiest.jsp";
        }
        if (!customerPojo.getApass().equals(customerPojo.getReapass())) {
            model.addAttribute("errorMsg", "两次密码输入不一致");
            return "forward:regiest.jsp";
        }
        if (null == customerPojo.getApass() || "" == customerPojo.getApass()) {
            model.addAttribute("errorMsg", "密码不能为空");
            return "forward:regiest.jsp";
        }

        customerService.regiest(customerPojo);

        return "login";
    }
    //注销登陆
    @RequestMapping("loginout")
    public String loginout(HttpSession session){
        session.removeAttribute("customer");
        return "login";
    }
    //修改个人信息
    @RequestMapping("updateCustomer")
    public ModelAndView updateCustomer(CustomerPojo customerPojo,HttpSession session){
        ModelAndView mv=new ModelAndView();
        if (null == customerPojo.getAname() || "" == customerPojo.getAname()) {
            mv.addObject("errorMsg", "用户名不能为空");
            mv.setViewName("updatecus");
            return mv;
        }
        CustomerPojo customerPojo1 = customerService.loregiest(customerPojo);
        if (null != customerPojo1) {
            mv.addObject("errorMsg", "用户名已存在");
            mv.setViewName("updatecus");
            return mv;
        }
        if (!customerPojo.getApass().equals(customerPojo.getReapass())) {
            mv.addObject("errorMsg", "两次密码输入不一致");
            mv.setViewName("updatecus");
            return mv;
        }
        if (null == customerPojo.getApass() || "" == customerPojo.getApass()) {
            mv.addObject("errorMsg", "密码不能为空");
            mv.setViewName("updatecus");
            return mv;
        }
        customerService.updatecus(customerPojo);
        mv.addObject("errorMsg","用户名密码修改成功");
        session.removeAttribute("customer");
        session.setAttribute("customer",customerPojo);
        mv.setViewName("updatecus");
        return mv;
    }
}
